/**
 * BeautifyActivator.java
 */
package com.pluginsacceptor.plugin.beautify;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import org.apache.log4j.Logger;

import com.pluginsacceptor.api.Activator;
import com.pluginsacceptor.plugin.beautify.model.BeautifyData;
import com.pluginsacceptor.plugin.beautify.model.CellList;
import com.pluginsacceptor.plugin.beautify.type.FontType;
import com.pluginsacceptor.utils.CommunicationUtils;


/**
 * The beautify (font) activator
 * This plugin will be inject to acceptor menu
 * This plugin sends information to another plugin (addObserver() - used)
 * This plugin doesn't receive information from another plugin (getObserver() - not used)
 *
 */

public class BeautifyActivator implements Activator {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(BeautifyActivator.class);

    /** */
    private JMenu menu;

    /** */
    private final DataSender dataSender = new DataSender();

    /** */
    private final BeautifyObserver beautifyObserver = new BeautifyObserver();

    /** */
    private final BeautifyData beautifyData = new BeautifyData();

    /**
     * {@inheritDoc}
     */
    @Override
    public JComponent activate(final String[] metadata) {
        LOG.info("Activate beautify plugin");
        createMenu();
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean deactivate() {
        LOG.info("Deactivate beautify plugin");
        this.dataSender.deleteObservers();
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addObserver(final Observer observer) {
        this.dataSender.addObserver(observer);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Observer getObserver() {
        return this.beautifyObserver;
    }


    @Override
    public JComponent getMenuItem() {
        return this.menu;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean load(final Object data) {
        boolean result = true;
        try {
            if (data instanceof List<?>) {
                this.beautifyData.clear();

                final List<String> loadData = (List<String>) data;
                int row, col;
                String colorName, fontType;
                for (final String dataItem : loadData) {
                    row = CommunicationUtils.getRowNumFromCellString(dataItem);
                    col = CommunicationUtils.getColNumFromCellString(dataItem);
                    colorName = CommunicationUtils.colorAsString(CommunicationUtils.getColorFromCellString(dataItem));
                    fontType = CommunicationUtils.getFontFromCellString(dataItem);
                    this.beautifyData.add(row, col, fontType, colorName);
                    this.dataSender.sendDate(row, col, fontType, colorName);
                }
            } else {
                LOG.warn("Invalid load object " + data);
                result = false;
            }
        } catch (final Exception e) {
            LOG.error("Load is failed", e);
            result = false;
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object save() {
        final List<Integer> rows = this.beautifyData.getCellList().getRows();
        final List<Integer> columns = this.beautifyData.getCellList().getColumns();
        final List<String> colorName = this.beautifyData.getColorNames();
        final List<String> fontType = this.beautifyData.getFontTypes();
        final int size = colorName.size();

        final List<String> savedData = new ArrayList<String>(size);
        for (int cnt = 0; cnt < size; cnt++) {
            savedData.add(
                CommunicationUtils.cellDisplayToString(
                    rows.get(cnt),
                    columns.get(cnt),
                    colorName.get(cnt),
                    fontType.get(cnt)
                )
            );
        }

        return savedData;
    }
    /**
     * Create menu
     */
    private void createMenu() {
        this.menu = new JMenu("Beautify");
        for (final FontType fontType : FontType.values()) {
            final JMenuItem fontMenuItem = new JMenuItem(fontType.name());
            fontMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    final CellList cellList = beautifyObserver.getActiveCellList();
                    dataSender.sendDate(cellList, fontType.name(), "");
                    beautifyData.add(cellList, fontType.name(), "");
                }
            });
            this.menu.add(fontMenuItem);
        }


        for (final Field f : Color.class.getFields()) {
            if (f.getType() == Color.class && f.getName().toLowerCase().equals(f.getName())) {
                final JMenuItem colorMenuItem = new JMenuItem(f.getName());

                colorMenuItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        final CellList cellList = beautifyObserver.getActiveCellList();
                        dataSender.sendDate(cellList, "", f.getName());
                        beautifyData.add(cellList, "", f.getName());
                    }
                });

                this.menu.add(colorMenuItem);
            }
        }

    }

}